﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Entity;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;



namespace ExaminationSys.View.Paper
{
    public partial class PaperFeedBackCommentList : System.Web.UI.Page
    {

        private static readonly PaperFeedBackCommentService Proxy = ObjectFactory<PaperFeedBackCommentService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!X.IsAjaxRequest)
                PageEntity = null;
        }
        protected void Page_Init(object sender, EventArgs e)
        {
            
        }

        private int pageSize = 20;
        public static PageEntity PageEntity = null;
        protected void PaperFeedBackComment_RefreshData(object sender, StoreReadDataEventArgs e)
        {
            string sqlWhere = " 1=1";
            int pageIndex = 0;
            if (PageEntity != null)
            {
                sqlWhere = PageEntity.SqlWhere;
                pageIndex = 1;
            }
            else
            {
                sqlWhere += string.Format(" and status<>0");
                pageIndex = (e.Start / e.Limit) + 1;

            }

            int totalPage, totalRecord;
            this.StorePaperFeedBackComment.DataSource = Proxy.GetListByPage(sqlWhere, " OperatorTime desc", pageSize, pageIndex,
                                                                   out totalPage, out totalRecord);
            e.Total = totalRecord;

            this.StorePaperFeedBackComment.DataBind();
        }

        public void SearchFun(PageEntity pageEntity)
        {
            PageEntity = pageEntity;
        }

        public void Btn_GoBack(object sender, DirectEventArgs e)
        {
            Response.Redirect("SectionDetailList.aspx");
        }

        protected void Command(object sender, DirectEventArgs e)
        {
            var command = e.ExtraParams["command"];

            var id = e.ExtraParams["Id"];
            if (string.IsNullOrWhiteSpace(id))
            {
                MessageBoxHelper.ShowInfo("获取ID失败");
                return;
            }

            if (command.Equals("edit", StringComparison.OrdinalIgnoreCase))
            {
                Response.Redirect("PaperFeedbackList.aspx?PaperFeedBackCommentID="+ id);
            }
            else
            {
                var entity = Proxy.Get(new Guid(id));
                if (entity != null)
                {
                    entity.Status = 0;
                    var success = Proxy.Update(entity);
                    if (success)
                    {
                        StorePaperFeedBackComment.Remove(entity.ID);
                        MessageBoxHelper.ShowInfo("删除成功");
                    }
                }
            }
        }

        protected void Add_Click(object sender, DirectEventArgs e)
        {
            Response.Redirect("PaperFeedbackList.aspx?PaperFeedBackCommentID="+ Guid.NewGuid());
        }

        protected void BtnGroupFind_Click(object sender, DirectEventArgs e)
        {
            var text = GroupSearchText.Text.Trim();
            if (!string.IsNullOrWhiteSpace(text))
            {
                text = text.SqlSafe();
                var sqlWhere = string.Format(" SerialNo like '%{0}%' or Title like '%{1}%' and  status<>0", text, text); 

                PageEntity = new PageEntity();
                PageEntity.SqlWhere = sqlWhere;
            }
            else
            {
                PageEntity = null;
            }
            StorePaperFeedBackComment.Reload();
        }


        [DirectMethod]
        public void DoConfirm()
        {
            RowSelectionModel sm = GridPanelPaperFeedBackComment.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }
            X.Msg.Confirm("提示", "是否删除选中记录?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "X.DoDelete()",
                    Text = "是"
                },
                No = new MessageBoxButtonConfig
                {
                    Text = "否"
                }
            }).Show();
        }
        [DirectMethod]
        public void DoDelete()
        {
            RowSelectionModel sm = GridPanelPaperFeedBackComment.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }

            List<string> ids = new List<string>();
            foreach (var row in sm.SelectedRows)
            {
                ids.Add(row.RecordID);
            }
            var success = Proxy.Delete(ids);
            if (success)
            {
                ids.ForEach(id => StorePaperFeedBackComment.Remove(id));
                MessageBoxHelper.ShowInfo("保存成功");
            }
        }
        protected void BatchDelete_Click(object sender, DirectEventArgs e)
        {
            RowSelectionModel sm = GridPanelPaperFeedBackComment.SelectionModel.Primary as RowSelectionModel;

            if (sm.SelectedRows.Count == 0)
            {
                MessageBoxHelper.ShowWarning("请选择要删除的记录");
                return;
            }
            List<string> ids = new List<string>();
            foreach (var row in sm.SelectedRows)
            {
                ids.Add(row.RecordID);
            }
            var success = Proxy.Delete(ids);
            if (success)
            {
                ids.ForEach(id => StorePaperFeedBackComment.Remove(id));
                MessageBoxHelper.ShowInfo("保存成功");
            }
        }
        protected void BatchLock_Click(object sender, DirectEventArgs e)
        {
            //RowSelectionModel sm = GridPanelPaperFeedBackCommentType.SelectionModel.Primary as RowSelectionModel;

            //if (sm.SelectedRows.Count == 0)
            //{
            //    MessageBoxHelper.ShowWarning("请选择要锁定的记录");
            //    return;
            //}
            //List<string> ids = new List<string>();
            //foreach (var row in sm.SelectedRows)
            //{
            //    ids.Add(row.RecordID);
            //}
            var success = true;// Proxy.Lock(ids, 2);
            if (success)
            {
                StorePaperFeedBackComment.Reload();
                MessageBoxHelper.ShowInfo("保存成功");
            }
        }

        protected void BatchUnLock_Click(object sender, DirectEventArgs e)
        {
            //RowSelectionModel sm = GridPanelPaperFeedBackCommentType.SelectionModel.Primary as RowSelectionModel;

            //if (sm.SelectedRows.Count == 0)
            //{
            //    MessageBoxHelper.ShowWarning("请选择要解锁的记录");
            //    return;
            //}
            //List<string> ids = new List<string>();
            //foreach (var row in sm.SelectedRows)
            //{
            //    ids.Add(row.RecordID);
            //}
            //var success = Proxy.Lock(ids, 1);
            //if (success)
            //{
            //    StorePaperFeedBackComment.Reload();
            //    MessageBoxHelper.ShowInfo("保存成功");
            //}
        }

        protected void BtnSearch_Click(object sender, DirectEventArgs e)
        {
            //  this.PaperFeedBackCommentSearchWindow.Show(SearchFun);
        }

        protected void ShowDetails(string id)
        {
            //var realId = new Guid(id);
            //var result = Proxy.Get(realId);
            //if (result != null)
            //{
            //    PaperFeedBackCommentEditWindow.GridStore = StorePaperFeedBackComment;
            //    this.PaperFeedBackCommentEditWindow.SetData(id, SectionID, EnumTestType);
            //    this.PaperFeedBackCommentEditWindow.Show();
            //}
        }
    }
}